﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;


namespace TranTekBE.KPI_NhanSu
{
    public partial class Them_Quy_Dinh_Xu_Phat : PagePopUp
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Load JS
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            if (!IsPostBack)
            {
                GetDropList_User(droplist_User_loc_kq);
                FunctionsUtils.BindDataForControlList_TienThuong(droplist_Tien_Phat, "", "Tiền Phạt");
                droplist_User_loc_kq.SelectedValue = Request.QueryString["user"];
                txt_thoi_gian_loc.Text = Request.QueryString["time"];
                txtNgay_them_vao.Text = DateTime.Now.ToShortDateString();
                
            }
            if (Request.QueryString["user"] == null && Request.QueryString["time"] == null)
            {
                LoadData("", "");
            }
            else
            {
                LoadData(Request.QueryString["user"], Request.QueryString["time"]);
            }
        }
        public static void GetDropList_User(DropDownList ddl)
        {
            DataSet ds_user = FunctionsUtils.Database2.ExecuteQuery("SELECT NAME_USER, HO_USER+' '+TEN_USER as NameUser FROM tbl_UserKPI where PHAN_QUYEN not in('user')");
            FunctionsUtils.BindDataForControlList(ddl, ds_user, "NAME_USER", "NameUser", "", "Chọn tên người tạo");
        }
        void LoadData(string nguoitao, string ngaytao)
        {
            string sql_loc = @"SELECT * FROM tbl_DropListLyDoPhat WHERE 1=1 ";
            string sql_user = "";
            string sql_date = "";
            if (droplist_User_loc_kq.SelectedValue.ToString() != "")
            {
                sql_user = " AND NGUOI_TAO = '" + nguoitao + "'";
            }
            if (txt_thoi_gian_loc.Text != "")
            {
                sql_date = " AND NGAY_TAO = '" + ngaytao + "'";
            }
            sql_loc = sql_loc + sql_user + sql_date;

            DataSet all_event = FunctionsUtils.Database2.ExecuteQuery(sql_loc);

            DataTable dt = all_event.Tables[0];

            PagedDataSource pgitems = new PagedDataSource();

            System.Data.DataView dv = new System.Data.DataView(dt);

            pgitems.DataSource = dv;

            pgitems.AllowPaging = true;

            pgitems.PageSize = 10;

            pgitems.CurrentPageIndex = PageNumber;

            if (pgitems.PageCount > 1)
            {

                rptPages.Visible = true;

                System.Collections.ArrayList pages = new System.Collections.ArrayList();

                for (int i = 0; i < pgitems.PageCount; i++)

                    pages.Add((i + 1).ToString());

                rptPages.DataSource = pages;

                rptPages.DataBind();

            }

            else

                rptPages.Visible = false;

            rpEvent.DataSource = pgitems;

            rpEvent.DataBind();

        }

        public int PageNumber
        {

            get
            {

                if (ViewState["PageNumber"] != null)

                    return Convert.ToInt32(ViewState["PageNumber"]);

                else

                    return 0;

            }

            set
            {

                ViewState["PageNumber"] = value;

            }

        }

        protected void rptPages_ItemCommand_Event(object source, RepeaterCommandEventArgs e)
        {

            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;

            if (Request.QueryString["user"] == null && Request.QueryString["time"] == null)
            {
                LoadData("", "");
            }
            else
            {
                LoadData(Request.QueryString["user"], Request.QueryString["time"]);
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        // Thêm Event
        protected void btnAddEvent_Click(object sender, EventArgs e)
        {
            if (txtEvent.Text == "" || droplist_Tien_Phat.SelectedIndex == 0 || txtNgay_them_vao.Text == "")
            {
                ltrErr.Text = "Lỗi! Vui lòng kiểm tra lại";
                ltrErr.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                string ten_event = txtEvent.Text;
                string tien_tru = droplist_Tien_Phat.SelectedValue.ToString();
                string ngay_them = txtNgay_them_vao.Text;
                string nguoi_them = UserInfo.UserName;
                try
                {
                    // Insert thông tin lịch làm việc người dùng 
                    string sql_add = @"
                                    INSERT INTO tbl_DropListLyDoPhat
                                    (NAME_LYDO,TIEN_TRU,NGAY_TAO,NGUOI_TAO)
                                    VALUES
                                    (N'" + ten_event + "'," + tien_tru + ",'" + ngay_them + "',N'" + nguoi_them + "')";

                    FunctionsUtils.Database2.ExecuteNonQuery(sql_add);
                    ltrErr.Text = "Thêm Thành Công!";
                    ltrErr.ForeColor = System.Drawing.Color.Red;
                }
                catch (Exception ex)
                {
                    ltrErr.Text = ex.Message;
                    ltrErr.ForeColor = System.Drawing.Color.Red;
                }
                Response.Redirect(Request.RawUrl);
            }

        }
        // Del Event
        protected void btnLoc_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["user"] == null && Request.QueryString["time"] == null)
            {
                LoadData("", "");
            }
            else
            {
                LoadData(Request.QueryString["user"], Request.QueryString["time"]);
            }
            
        }

        protected void droplist_User_loc_kq_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Them-Quy-Dinh-Xu-Phat.aspx?user=" + droplist_User_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text);
        }

        protected void txt_thoi_gian_loc_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("Them-Quy-Dinh-Xu-Phat.aspx?user=" + droplist_User_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text);
        }
        //========= Delete buttons inside repeater.
        protected void rpEventDetails_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case ("Delete"):
                    int id = Convert.ToInt32(e.CommandArgument);
                    deleteEvent(id);
                    break;
            }
            Response.Redirect("Them-Quy-Dinh-Xu-Phat.aspx");
        }

        //===== Method to delete employee from database.
        void deleteEvent(int id)
        {
            //======= Delete Query.
            string cmdText_del = "DELETE FROM tbl_DropListLyDoPhat WHERE ID_LYDO="+id;
            FunctionsUtils.Database2.ExecuteNonQuery(cmdText_del);
            
        }

       
        
    }
}